using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Generic;

namespace Nate.GUI.Canvas.Component
{
    public class PolyInsert : IComponent
    {
        public PolyInsert(NavMesh navmesh_canvas)
        {
            _navmesh_canvas = navmesh_canvas;
            _edge = null;
        }

        public void InitializeComponent()
        {
            _navmesh_canvas.MouseClick += new MouseEventHandler(_MouseClick);
            _navmesh_canvas.KeyDown += new KeyEventHandler(_KeyDown);
            _navmesh_canvas.KeyUp += new KeyEventHandler(_KeyUp);
        }

        public void Tick()
        {
        }
        
        private void  _KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control)
            {
                _ctrl_key_down = true;
            }
        }

        private void  _KeyUp(object sender, KeyEventArgs e)
        {
            if (_ctrl_key_down && e.Control)
            {
                _ctrl_key_down = false;
            }
        }

        private void _MouseClick(object sender, MouseEventArgs e)
        {
            if (MouseButtons.Left == e.Button)
            {
                if (!_ctrl_key_down)
                {
                    Edge = null;
                    return;
                }

                NavMesh.Edge edge_temp = null;
                _navmesh_canvas.Bucket.Find(new PointF((float)e.Location.X, (float)e.Location.Y), out edge_temp);
                Edge = edge_temp;
                return;
            }

            if (MouseButtons.Right == e.Button)
            {
                if (null == Edge)
                {
                    return;
                }

                // Create a new poly from the two verts

                NavMesh.Vertex v    = _navmesh_canvas.Bucket.CreateVertex(new PointF((float)e.Location.X, (float)e.Location.Y));
                NavMesh.Edge e1     = _navmesh_canvas.Bucket.CreateEdge(Edge.A, v.Id);
                NavMesh.Edge e2     = _navmesh_canvas.Bucket.CreateEdge(Edge.B, v.Id);
                NavMesh.Poly p      = _navmesh_canvas.Bucket.CreatePoly();
                
                p.Edges.Add(Edge.Id);
                p.Edges.Add(e1.Id);
                p.Edges.Add(e2.Id);

                _navmesh_canvas.Bucket.Add(v);
                _navmesh_canvas.Bucket.Add(e1);
                _navmesh_canvas.Bucket.Add(e2);
                _navmesh_canvas.Bucket.Add(p);
            }
        }

        NavMesh.Edge Edge
        {
            get
            {
                return _edge;
            }
            set
            {
                if (null != _edge)
                {
                    _edge.Highlighted = false;
                }

                if (null != value)
                {
                    value.Highlighted = true;
                }

                _edge = value;
            }
        }


        private bool _ctrl_key_down;
        private NavMesh _navmesh_canvas;
        NavMesh.Edge _edge;
    }
}